package com.example.demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.SysUserMapper;
import com.example.demo.pojo.LoginUser;
import com.example.demo.pojo.SysUser;
import com.example.demo.service.SysUserService;
import com.example.demo.uilt.JwtUtil;
import com.example.demo.uilt.RedisCache;
import com.example.demo.uilt.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Objects;

/**
 *
 */
@Service
public class SysUserServiceImpl
    extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService
{

  @Autowired
  private AuthenticationManager authenticationManager;

  @Autowired
  private RedisCache redisCache;
  @Autowired
 private  SysUserService sysUserService;

  @Override
  public ResponseResult login(SysUser sysUser) {
    //使用 Authentication 对象 封装 登录用户信息
             UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(sysUser.getLoginName(), sysUser.getPassword());

    // this.getAuthenticationManager().authenticate(authRequest);
    //   使用 Authentication 对象 封装 登录用户信息
    Authentication authenticate;
    try {
      authenticate = authenticationManager.authenticate(authenticationToken);
    } catch (AuthenticationException e) {
      throw new RuntimeException("用户名或密码错误");
    }

    if(Objects.isNull(authenticate)){
      throw new RuntimeException("用户名或密码错误");
    }
    // 前后端分离项目，后端要自己维护session，也就是token
    //使用userid生成token
    LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
    String userId = loginUser.getSysUser().getUserId().toString();

    String jwt = JwtUtil.createJWT(userId);

    //authenticate存入redis
    redisCache.setCacheObject("login:"+userId,loginUser);

    //把token响应给前端
    HashMap<String,String> map = new HashMap<>();
    map.put("token",jwt);

    return new ResponseResult(200,"登陆成功",map);
  }

  @Override
  public ResponseResult userLists(SysUser sysUser, Integer pageIndex, Integer pageSize) {
    IPage<SysUser> page=new Page<SysUser>(pageIndex,pageSize);
    QueryWrapper<SysUser> queryWrapper=new QueryWrapper();
    queryWrapper.eq(sysUser.getLoginName()!=null&&sysUser.getLoginName()!="","login_name",sysUser.getLoginName());
    queryWrapper.eq(sysUser.getPhonenumber()!=null&&sysUser.getPhonenumber()!="","phonenumber",sysUser.getPhonenumber());
    queryWrapper.eq(sysUser.getStatus()!=null&&sysUser.getStatus()!=-1,"status",sysUser.getStatus());
    IPage<SysUser> list=sysUserService.page(page,queryWrapper);

//    ResponseResult responseResult = new ResponseResult(200, "userLists");
    HashMap hashMap=new HashMap();
    hashMap.put("userList",list);
//   responseResult.put("userList",list);
    if(list!=null){
      return new ResponseResult(200,"成功",hashMap);
    }else {
      return new ResponseResult(500,"shibai",hashMap);
    }

  }

}
